<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人主页</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: Arial, sans-serif;
            background: #f5f5f5;
            margin: 0;
            padding: 20px;
        }
        
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border: 1px solid #ddd;
        }
        
        .header {
            background: #007bff;
            color: white;
            padding: 20px;
            text-align: center;
        }
        
        .header h1 {
            font-size: 1.5em;
            margin-bottom: 10px;
        }
        
        .avatar-section {
            text-align: center;
            padding: 30px;
            border-bottom: 1px solid #eee;
        }
        
        .avatar {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            object-fit: cover;
            border: 2px solid #007bff;
            margin-bottom: 15px;
        }
        
        .default-avatar {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            background: #6c757d;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 3em;
            color: white;
            margin: 0 auto 15px;
        }
        
        .upload-btn {
            background: #28a745;
            color: white;
            border: none;
            padding: 8px 16px;
            cursor: pointer;
            font-size: 14px;
        }
        
        .upload-btn:hover {
            background: #1e7e34;
        }
        
        .user-info {
            padding: 30px;
        }
        
        .info-item {
            display: flex;
            margin-bottom: 15px;
            padding: 10px;
            background: #f8f9fa;
            border: 1px solid #e9ecef;
        }
        
        .info-label {
            font-weight: bold;
            color: #333;
            min-width: 80px;
        }
        
        .info-value {
            color: #666;
            flex: 1;
        }
        
        .actions {
            padding: 30px;
            text-align: center;
            border-top: 1px solid #eee;
        }
        
        .btn {
            display: inline-block;
            padding: 10px 20px;
            margin: 10px;
            background: #007bff;
            color: white;
            text-decoration: none;
            font-size: 14px;
            border: none;
            cursor: pointer;
        }
        
        .btn:hover {
            background: #0056b3;
        }
        
        .btn-secondary {
            background: #6c757d;
        }
        
        .btn-secondary:hover {
            background: #545b62;
        }
        
        .message {
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 20px;
            text-align: center;
        }
        
        .success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .hidden {
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>个人主页</h1>
            <p>欢迎回来，<span th:text="${user.username}">用户名</span>！</p>
        </div>
        
        <div class="avatar-section">
            <div id="message"></div>
            
            <div th:if="${user.avatarUrl}" class="avatar-container">
                <img th:src="${user.avatarUrl}" alt="头像" class="avatar">
            </div>
            <div th:unless="${user.avatarUrl}" class="avatar-container">
                <div class="default-avatar">?</div>
            </div>
            
            <input type="file" id="avatarInput" accept="image/*" class="hidden">
            <button class="upload-btn" onclick="document.getElementById('avatarInput').click()">
                上传头像
            </button>
        </div>
        
        <div class="user-info">
            <div class="info-item">
                <div class="info-label">用户名：</div>
                <div class="info-value" th:text="${user.username}">用户名</div>
            </div>
            
            <div class="info-item">
                <div class="info-label">邮箱：</div>
                <div class="info-value" th:text="${user.email}">邮箱</div>
            </div>
            
            <div class="info-item">
                <div class="info-label">手机号：</div>
                <div class="info-value" th:text="${user.phone}">手机号</div>
            </div>
            
            <div class="info-item">
                <div class="info-label">注册时间：</div>
                <div class="info-value" th:text="${#temporals.format(user.createdTime, 'yyyy-MM-dd HH:mm:ss')}">注册时间</div>
            </div>
        </div>
        
        <div class="actions">
            <a href="/logout" class="btn btn-secondary">退出登录</a>
        </div>
    </div>

    <script>
        function showMessage(message, type) {
            const messageDiv = document.getElementById('message');
            messageDiv.innerHTML = message;
            messageDiv.className = 'message ' + type;
        }
        
        document.getElementById('avatarInput').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (!file) return;
            
            // 检查文件类型
            if (!file.type.startsWith('image/')) {
                showMessage('只能上传图片文件', 'error');
                return;
            }
            
            // 检查文件大小（5MB）
            if (file.size > 5 * 1024 * 1024) {
                showMessage('文件大小不能超过5MB', 'error');
                return;
            }
            
            const formData = new FormData();
            formData.append('file', file);
            
            fetch('/uploadAvatar', {
                method: 'POST',
                body: formData
            })
            .then(response => response.text())
            .then(data => {
                if (data === '头像上传成功') {
                    showMessage('头像上传成功！', 'success');
                    // 刷新页面显示新头像
                    setTimeout(() => {
                        location.reload();
                    }, 1500);
                } else {
                    showMessage(data, 'error');
                }
            })
            .catch(error => {
                showMessage('上传失败，请重试', 'error');
            });
        });
    </script>
</body>
</html>
